﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LTM_Extra;

namespace LTM_Main
{
    public partial class ManagingAuthors : Form
    {
        public ManagingAuthors()
        {
            InitializeComponent();
        }

        ContentProvider contentProvider = new ContentProvider();
        private void btnAddAuthor_Click(object sender, EventArgs e)
        {
            try
            {

                AuthorCreation authorCreation = new AuthorCreation(contentProvider);
                authorCreation.ShowDialog();
                BindlstManagingAuthor();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }


        }

        private void BindlstManagingAuthor()
        {
            var authorList = contentProvider.GetAuthorList();
            lstManagingAuthor.DataSource = authorList;
        }


        private void btnDeleteAuthor_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure to delete this author?", "Alert", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    contentProvider.DeleteAuthor(lstManagingAuthor.SelectedValue.ToString());
                    BindlstManagingAuthor();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void UpdateAuthor()
        {
            try
            {
                if (lstManagingAuthor.SelectedValue == null)
                {
                    return;
                }
                contentProvider.UpdateAuthor(lstManagingAuthor.SelectedValue.ToString(), Helper.GetPropertiesFromTableLayout(tlpAuthor));
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void lstManagingAuthor_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Helper.InitTableLayoutPanelControlsValue(tlpAuthor, contentProvider.GetAuthorProperties(lstManagingAuthor.SelectedValue.ToString()));
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void ManagingAuthors_Load(object sender, EventArgs e)
        {
            try
            {
                Helper.InitTableLayoutPanel(tlpAuthor, contentProvider.GetAuthorVariables());
                BindlstManagingAuthor();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateAuthor();
                contentProvider.SaveAuthor();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateAuthor();
                contentProvider.SaveAuthor();
                Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
